<?php
##############################################
#
#   首页
# @ Time : 2017-1-25 - 下午10:07:21
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################

namespace Home\Controller;
use Think\Controller;
use Home\Model\UserModel;

class RegController extends Controller{
	
	public function index(){
		$this -> display();
	}
	
	public function showCode(){
		$config =    array(    
			'fontSize'    =>    15,    // 验证码字体大小
			'length'      =>    4,     // 验证码位数
			'useNoise'    =>    false, // 关闭验证码杂点
		);
		$Verify =     new \Think\Verify($config);
		$Verify->entry();
	}
	
	public function check(){
		if(!IS_AJAX) return false;
		$key = key(I('post.'));
		$value = I("post.$key"); //addslashes — 使用反斜线引用字符串
		$user_model = new UserModel();
		switch ($key){
			case 'email':
				if($user_model -> check('email', $value)){
					$result = array('status' => false, 'message' => '该邮箱已注册');
				}else{
					$result = array('status' => true);
				}
				break;
			case 'username':
				if($user_model -> check('uname', $value)){
					$result = array('status' => false, 'message' => '用户名已存在');
				}else{
					$result = array('status' => true);
				}
				break;
			case 'code':
				if(!$this -> checkCode($value)){
					$result = array('status' => false, 'message' => '验证码错误');
				}else{
					$result = array('status' => true);
				}
				break;
		}
		$this -> ajaxReturn($result);
	}
	
	//验证码检测，check()方法检测验证码的输入是否正确
	private function checkCode($code){
		$Verify = new \Think\Verify();
		return $Verify -> check($code);
	}
	
	/**
	 * 注册用户
	 */
	public function regUser(){
		if(!IS_POST) return;
		$auto = I('post.auto');
		if(!empty($auto)){
			$value = 'houdongwang';
			$value = encryption($value);
			echo $value;
			$value = encryption($value, 1);
		}
		exit;
		$data['email'] = I('post.email');
		$data['uname'] = I('post.username');
		$data['password'] = md5(I('post.password'));
		$user_model = new UserModel();
		$uid = $user_model -> regUser($data);
		
		if($uid){
			$_SESSION['uid'] = $uid;
			$_SESSION['username'] = $data['uname'];
			$this -> success('注册成功', U('Index/index'), 2);
		}else{
			$this -> error('注册失败', '', 2);
		}
	}
}